package controller;

import entity.r_complaint;
import entity.r_food;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import service.FoodService;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/food")
public class FoodController {
    @Resource
    private FoodService foodService;
    @RequestMapping("/foodList")
    public String foodList(@RequestParam(required = false) String food_Name,
                           @RequestParam(required = false) String food_type,Model model){
        r_food food = new r_food();
        if (food_type==null){
            food_type="0";
        }
        System.out.println(food_Name);
        System.out.println(food_type);
        food.setRf_food_Name(food_Name);
        food.setRf_food_Type(Integer.parseInt(food_type));
        List<r_food> list = this.foodService.getAll(food);
        model.addAttribute("list",list);
        model.addAttribute("Name",food_Name);
        model.addAttribute("type",food_type);
        return "food/basic";
    }
    @RequestMapping("/compList")
    public String complaintsSearch(@RequestParam(required = false) String roomname,
                                   @RequestParam(required = false) String roomstar,
                                   @RequestParam(required = false) String pastTime,
                                   @RequestParam(required = false) String futureTime,
                                   HttpSession session,
                                   Model model){
        if("0".equals(roomstar)){
            roomstar="";
        }
        model.addAttribute("roomname",roomname);
        model.addAttribute("roomstar",roomstar);
        model.addAttribute("pastTime",pastTime);
        model.addAttribute("futureTime",futureTime);
        System.out.println(roomname+","+roomstar+","+pastTime+","+futureTime);
        Map<String,Object> map = new HashMap<String,Object>();
        SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd");
        if(pastTime!=null
                &&pastTime!=""){
            try {
                Date pastTime1=sdf.parse(pastTime);
                map.put("pastTime",pastTime1);
            } catch (ParseException e) {
                e.printStackTrace();
            }
        }else{
            map.put("pastTime","");
        }
        if(futureTime!=null
                &&futureTime!=""){
            try {
                Date futureTime1=sdf.parse(futureTime);
                map.put("futureTime",futureTime1);
            } catch (ParseException e) {
                e.printStackTrace();
            }
        }else{
            map.put("futureTime","");
        }
        map.put("roomname",roomname);
        map.put("roomstar",roomstar);
        System.out.println(map);
        List<r_complaint> list = this.foodService.getCompAll(map);
        model.addAttribute("complainList",list);
        return "food/complaintsSearch";
    }
}
